package com.fqcar.fqdf.service;

import android.app.Activity;
import android.content.Context;

import com.android.volley.Response;
import com.fqcar.fqdf.RemoteAPI;
import com.fqcar.fqdf.core.http.MultipartOptRequest;
import com.fqcar.fqdf.core.http.MultipartTOptRequest;
import com.fqcar.fqdf.core.http.ResponseEntityOptRequest;
import com.fqcar.fqdf.core.http.ResponseTOptRequest;
import com.fqcar.fqdf.core.http.UrlHelper;
import com.fqcar.fqdf.entity.CarListReqBean;
import com.fqcar.fqdf.entity.ResponseEntity;
import com.fqcar.fqdf.util.SharedPreferenceUtil;
import com.fqdf.framework.android.util.JsonUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvsidong on 16/3/2.
 */
public class RequestOptService {


    /**
     * 系统参数
     *
     * @param act      上下文内容
     * @param listener 请求成功回调接口
     */
    public static void sysSetting(Context act, String value, Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("key", value);
        requestBase(getUrl(act, RemoteAPI.SETTING), params, getAuthHeader(act), listener, errorListener);
    }
    /**
     * 验证token
     *
     * @param act      上下文内容
     * @param listener 请求成功回调接口
     */
    public static void tokenValidate(Context act, Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("pushToken", SharedPreferenceUtil.getStringSPAttrs(act, SharedPreferenceUtil.AttrInfo.PROP_GT_PUSH_CLIENT_ID, ""));

        requestBase(getUrl(act, RemoteAPI.TOKEN_VALIDATE), params, getAuthHeader(act), listener, errorListener);
    }


    /**
     * 登录
     *
     * @param act      上下文内容
     * @param mobile
     * @param password
     * @param listener 请求成功回调接口
     */
    public static <T> void login(Activity act, String mobile, String password, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("password", password);
        requestBaseT(getUrl(act, RemoteAPI.LOGIN), params, getAuthHeader(act), listener, errorListener);
    }
    /*public static void login(Context act, String mobile, String password, Response.Listener<ResponseEntity> listener, GofunErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("password", password);
        ResponseEntityRequest request = new ResponseEntityRequest(RemoteAPI.LOGIN, params, listener, errorListener, act);
        VolleySingleton.getInstance().addRequest(request);
    }*/
/*
    public static void login(Context act, String mobile, String password, Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("password", password);
        requestBase(getUrl(act, RemoteAPI.LOGIN), params, getAuthHeader(act), listener, errorListener);
    }
*/

    /**
     * 获取首页车辆列表
     * @param act
     * @param reqBean
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getMainCarList(Context act, CarListReqBean reqBean, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.MAIN_GET_CAR_LIST), JsonUtil.javaObjToMap(reqBean), getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取可用站点区域
     * @param <T>
     * @param act
     * @param listener
     */
    public static <T> void getUseableStation(Context act, Response.Listener<T> listener,Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.CAR_GET_USEABLE_STATION), null, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取搜索结果
     * @param act
     * @param carNum
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getSearchCarList(Context act, String carNum, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carNum", carNum);
        requestBaseT(getUrl(act, RemoteAPI.SEARCH_CAR), params, getAuthHeader(act), listener, errorListener);
    }
    /**
     * 获取车辆列表（列表模式）
     * @param act
     * @param reqBean
     * @param listener
     * @param errorListener
     */
    public static <T> void getCarListPageNum(Context act, CarListReqBean reqBean, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.MAIN_GET_CAR_DETAIL_LIST), JsonUtil.javaObjToMap(reqBean), getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取待办车辆列表
     * @param act
     * @param pageNum
     * @param cause
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getToDoCarListPageNum(Context act, int pageNum,String cause, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("pageNum", String.valueOf(pageNum));
        params.put("pageSize", "20");
        params.put("cause", cause);
        requestBaseT(getUrl(act, RemoteAPI.TO_DO_CAR_LIST), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 车辆历史处理
     * @param act
     * @param carId
     * @param pageNum
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getHistoryCarListPageNum(Context act, int carId, int pageNum, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("pageNum", String.valueOf(pageNum));
        params.put("pageSize", "20");
        params.put("carId", String.valueOf(carId));
        requestBaseT(getUrl(act, RemoteAPI.GET_DEAL_HISTORY), params, getAuthHeader(act), listener, errorListener);
    }


    /**
     * 获取车辆详情
     * @param act
     * @param carId
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     */
    public static <T> void getCarDetailInfo(Context act,int carId, double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.CAR_DETAIL), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取开始处理原因
     * @param act
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getStartDealPage(Context act,int carId, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        requestBaseT(getUrl(act, RemoteAPI.GET_START_DEAL_PAGE), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 结束车辆上线
     * @param act
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void endDealAndOnLine(Context act,int carId,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.END_DEAL_AND_ONLINE), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 下线车辆
     * @param act
     * @param carId
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void offLine(Context act,int carId,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.OFF_LINE), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 结束车辆不上线（原因）
     * @param act
     * @param carId
     * @param cause
     * @param detail
     * @param lat
     * @param lng
     * @param imgUrls 数组的话用 ， 隔开
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void endDeal(Context act,int carId,String cause,String detail,double lat,double lng,String imgUrls, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("cause", cause);
        params.put("detail", detail);
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        params.put("imgUrls", imgUrls);
        requestBaseT(getUrl(act, RemoteAPI.END_DEAL), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 上报问题
     * @param act
     * @param license
     * @param detail
     * @param imgUrls
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void reportProblems(Context act,String carNum,String detail,String imgUrls,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carNum", carNum);
        params.put("detail", detail);
        params.put("imgUrls", imgUrls);
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.REPORT_PROBLEM), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取未完成处理订单
     * @param act
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getUnfinishDeal(Context act, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.GET_UNFINISH_DEAL), null, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 处理详情页面
     * @param act
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getDealDetailPage(Context act, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.GET_DEAL_DETAIL_PAGE), null, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 开始处理
     * @param act
     * @param carId
     * @param cause
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void startDeal(Context act,int carId,String cause,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("cause", cause);
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.START_DEAL), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取结束处理原因
     * @param act
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getEndDealPage(Context act, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        requestBaseT(getUrl(act, RemoteAPI.GET_END_DEAL_PAGE), null, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 充电
     * @param act
     * @param carId
     * @param qrCode
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void confirmCharge(Context act, int carId,String qrCode,Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("qrCode", qrCode);
        requestBaseT(getUrl(act, RemoteAPI.CONFIRM_CHARGE), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 车辆控制
     * @param act
     * @param carId
     * @param operate
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void carControl(Context act,int carId,String operate,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("operate", operate);
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.CAR_CONTROL), params, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 手动更改车辆位置
     * @param act
     * @param carId
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void updateCarPosition(Context act,int carId,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("carId", String.valueOf(carId));
        params.put("lat", String.valueOf(lat));
        params.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.UPDATE_CAR_POSITION), params, getAuthHeader(act), listener, errorListener);
    }
    /**
     * 获取城市列表
     *
     * @param act
     * @param listener
     * @param errorListener
     */
    /*public static void getCityList(Context act,double lat,double lng, Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        Map<String, String> param = new HashMap<>();
        param.put("lat", String.valueOf(lat));
        param.put("lng", String.valueOf(lng));
        requestBase(getUrl(act, RemoteAPI.GET_CITY_LIST), param, getAuthHeader(act), listener, errorListener);
    }*/
    public static <T> void getCityList(Context act,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        Map<String, String> param = new HashMap<>();
        param.put("lat", String.valueOf(lat));
        param.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.GET_CITY_LIST), param, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取充电桩界面列表
     * @param act
     * @param lat
     * @param lng
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getChargeList(Context act, double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        Map<String, String> param = new HashMap<>();
        param.put("lat", String.valueOf(lat));
        param.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.GET_CHARGE_LIST), param, getAuthHeader(act), listener, errorListener);
    }

    /**
     * 获取充电桩详情
     * @param act
     * @param stubId
     * @param listener
     * @param errorListener
     * @param <T>
     */
    public static <T> void getChargeDetail(Context act, int stubId,double lat,double lng, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        Map<String, String> param = new HashMap<>();
        param.put("stubId", String.valueOf(stubId));
        param.put("lat", String.valueOf(lat));
        param.put("lng", String.valueOf(lng));
        requestBaseT(getUrl(act, RemoteAPI.GET_CHARGE_DETAIL), param, getAuthHeader(act), listener, errorListener);
    }


    /**
     * 请求上传的基本类
     *
     * @param url           请求的url
     * @param params        请求的参数
     * @param headers       请求的header
     * @param fileList      请求的fileList数据
     * @param listener      请求成功回调接口      请求成功回调
     * @param errorListener 请求失败回调接口 请求失败的回调
     */
    public static void requestUploadsBase(String url, Map<String, String> params, Map<String, String> headers, List<File> fileList,
                                          Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        MultipartOptRequest request = new MultipartOptRequest(url, params, headers, fileList, listener, errorListener);
        VolleySingleton.getInstance().addRequest(request);
    }

    /**
     * 请求上传的基本类
     *
     * @param url           请求的url
     * @param params        请求的参数
     * @param headers       请求的header
     * @param file          请求的file数据
     * @param listener      请求成功回调接口      请求成功回调
     * @param errorListener 请求失败回调接口 请求失败的回调
     */
    public static void requestUploadBase(String url, Map<String, String> params, Map<String, String> headers, File file,
                                         Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        MultipartOptRequest request = new MultipartOptRequest(url, params, headers, file, listener, errorListener);
        VolleySingleton.getInstance().addRequest(request);
    }

    public static <T> void requestUploadBaseT(String url, Map<String, String> params, Map<String, String> headers, File file,
                                              Response.Listener<T> listener, Response.ErrorListener errorListener) {
        MultipartTOptRequest request = new MultipartTOptRequest(url, params, headers, file, listener, errorListener);
        VolleySingleton.getInstance().addRequest(request);
    }

    /**
     * 请求基本类
     *
     * @param url           请求的url
     * @param params        请求的参数
     * @param headers       请求的header
     * @param listener      请求成功回调接口      请求成功回调
     * @param errorListener 请求失败回调接口 请求失败的回调
     */
    public static void requestBase(String url, Map<String, String> params, Map<String, String> headers,
                                   Response.Listener<ResponseEntity> listener, Response.ErrorListener errorListener) {
        ResponseEntityOptRequest request = new ResponseEntityOptRequest(url, params, headers, listener, errorListener);
        VolleySingleton.getInstance().addRequest(request);
    }

    public static <T> void requestBaseT(String url, Map<String, String> params, Map<String, String> headers,
                                        Response.Listener<T> listener, Response.ErrorListener errorListener) {
        ResponseTOptRequest<T> request = new ResponseTOptRequest<>(url, params, headers, listener, errorListener);
        VolleySingleton.getInstance().addRequest(request);
    }

    /**
     * 获取auth的header,用来为以后的工具类提供方便暂时留着
     *
     * @param context 上下文内容
     * @return 附带auto的headmap
     */
    public static Map<String, String> getAuthHeader(Context context) {

        String authCode = SharedPreferenceUtil.getStringSPAttrs(context, SharedPreferenceUtil.AttrInfo.USER_TOKEN, "");

        Map<String, String> result = new HashMap<>();
        result.put("Authorization", authCode);
        return result;
    }

    /**
     * 获取拼接的URL
     *
     * @param context   内容上下文
     * @param urlAction 方法url
     * @return
     */
    public static String getUrl(Context context, String urlAction) {
//        BDLocation curLocation = ((DFApp) context.getApplicationContext()).getCurLocation();
        return UrlHelper.getInstance(context).getAppendUrl(urlAction);
    }


}
